
import java.util.Vector;

/*
 * DatabaseGui.java
 *
 * Created on February 25, 2009, 9:07 AM
 */
import java.util.logging.Level;
import java.util.logging.Logger;



/**
 *
 * @author  Justin
 */
public class DatabaseGui extends javax.swing.JFrame {
    
    private Database db;

    /** Creates new form DatabaseGui */
    public DatabaseGui(Database database) throws Exception {
        db = database;
        initComponents();
        //initialize the results table to the first relation in the database
        resultsTable.setModel(db.relation(0));
        Vector relationNames = new Vector();
        
        //place the names of all the relations into the combo box to select from
        for(int i = 0; i < db.numberOfRelations(); i++){
            relationNames.add(db.relation(i).relationName());
        }
        relationSelectCB.setModel(new javax.swing.DefaultComboBoxModel(relationNames));
        fromCB.setModel(new javax.swing.DefaultComboBoxModel(relationNames));
        relationNames.insertElementAt("None", 0);
        fromCB1.setModel(new javax.swing.DefaultComboBoxModel(relationNames));
        show();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        relationSelectL = new javax.swing.JLabel();
        relationSelectCB = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        resultsTable = new javax.swing.JTable();
        queryB = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        andL = new javax.swing.JLabel();
        fromCB = new javax.swing.JComboBox();
        fromL = new javax.swing.JLabel();
        fromCB1 = new javax.swing.JComboBox();
        selectL = new javax.swing.JLabel();
        selectCB = new javax.swing.JComboBox();
        whereL = new javax.swing.JLabel();
        whereTF = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        relationSelectL.setText("Relation Select");

        relationSelectCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                relationSelectCBActionPerformed(evt);
            }
        });

        resultsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(resultsTable);

        queryB.setText("Query");
        queryB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                queryBActionPerformed(evt);
            }
        });

        jLabel1.setText("Query Parameters");

        andL.setText("And:");

        fromCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fromCBActionPerformed(evt);
            }
        });

        fromL.setText("From:");

        selectL.setText("Select:");

        whereL.setText("Where:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(fromL)
                                    .addComponent(selectL)
                                    .addComponent(andL))
                                .addGap(30, 30, 30)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(fromCB1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(selectCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(fromCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(104, 104, 104))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(whereTF, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(whereL))
                        .addGap(47, 47, 47))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(queryB)))
                .addGap(113, 113, 113))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(relationSelectL)
                    .addComponent(relationSelectCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(614, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(relationSelectL)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(relationSelectCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(fromCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(fromL))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(fromCB1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(andL))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(selectCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(selectL))
                        .addGap(37, 37, 37)
                        .addComponent(whereL)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(whereTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(37, 37, 37)
                        .addComponent(queryB)))
                .addContainerGap(56, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void relationSelectCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_relationSelectCBActionPerformed
    String selectedRelation = (String)relationSelectCB.getSelectedItem();
        try {
            resultsTable.setModel(db.relation(selectedRelation));//GEN-LAST:event_relationSelectCBActionPerformed
        } catch (Exception ex) {
            Logger.getLogger(DatabaseGui.class.getName()).log(Level.SEVERE, null, ex);
        }
}

private void queryBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_queryBActionPerformed
    String from;
    String from1;
    String select;
    String where;
    Relation answer = new Relation();
    
    from = (String)fromCB.getSelectedItem();
    from1 = (String)fromCB1.getSelectedItem();
    select = (String)selectCB.getSelectedItem();
    where = whereTF.getText();
    
    if (from1.equals("None")){
            try {
                answer = db.query(select, from, where);
            } catch (Exception ex) {
                Logger.getLogger(DatabaseGui.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
    else {
            try {
                answer = db.query(select, from, from1, where);
            } catch (Exception ex) {
                Logger.getLogger(DatabaseGui.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
    
    resultsTable.setModel(answer);
    String newName = from + "_modified";
    answer.setRelationName(newName);
    db.addRelation(answer);
    relationSelectCB.addItem(answer.relationName());
    
}//GEN-LAST:event_queryBActionPerformed

private void fromCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fromCBActionPerformed
        try {

// TODO add your handling code here:
            Relation selected = db.relation((String) fromCB.getSelectedItem());

            Vector<String> columnNames = new Vector();//GEN-LAST:event_fromCBActionPerformed
            for (int i = 0; i < selected.getColumnCount(); i++) {
                columnNames.add(selected.getColumnName(i));
            }

            columnNames.insertElementAt("*", 0);
            selectCB.setModel(new javax.swing.DefaultComboBoxModel(columnNames));
        } catch (Exception ex) {
            Logger.getLogger(DatabaseGui.class.getName()).log(Level.SEVERE, null, ex);
        }
}



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel andL;
    private javax.swing.JComboBox fromCB;
    private javax.swing.JComboBox fromCB1;
    private javax.swing.JLabel fromL;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton queryB;
    private javax.swing.JComboBox relationSelectCB;
    private javax.swing.JLabel relationSelectL;
    private javax.swing.JTable resultsTable;
    private javax.swing.JComboBox selectCB;
    private javax.swing.JLabel selectL;
    private javax.swing.JLabel whereL;
    private javax.swing.JTextField whereTF;
    // End of variables declaration//GEN-END:variables

}
